/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package csBakery;

import java.io.*;
import java.net.URL;
import java.text.DateFormat;
import java.util.Date;
import java.util.Vector;
import java.lang.String;

/*
 * 	@release Final Release
 *	@author  Leung Ho Yan
 *	@version 5.1.3
 *
 * 	@release Release 2
 *	@author  Yip Tak Wai
 *	@version 4.0.1
 *
 * 	@release Release 1
 *	@author  Leung Ho Yan
 *	@version 1.0.0
 */

public class StockController {
	
	private Stock stocks;
	private final String defaultLink = "stock.txt";
	private String thisLink;
	public StockController(){
		stocks = new Stock();
		thisLink = defaultLink;
		loadStockRecord();
	} 
	
	public StockController(String filename){
		stocks = new Stock();
		thisLink = filename;
		loadStockRecord();
	}
	
	public Vector<String> getStockProductList(){
		return stocks.getStockProductList();
	}
	
	public void updateStock(LogController logController, String name, int n){
		int tempStock = stocks.getProductQuantity(name);
		tempStock += n;
		stocks.setProductQuantity(name, tempStock);
		logController.addLog(name, LogController.TYPE_STOCK, n);
	}
	
	public boolean updateSale(LogController logController, String name, int n){
		int tempStock = stocks.getProductQuantity(name);
		if (n > tempStock){
			return false;
		}
		else {
			stocks.setProductQuantity(name, tempStock - n);
			logController.addLog(name, LogController.TYPE_SALE, n);
			return true;
		}
	}

	public void stockState(){
		System.out.println("-----Stock State-----");
		System.out.println("Current Stock: ");
		System.out.println(stocks);
	}

	public Stock getStocks() {
		return stocks;
	}
	
	public void saveStockRecord()
	{
		try{
			File logFile = new File(thisLink);
			if (!logFile.exists()){
				logFile.createNewFile();
			}
			FileOutputStream fileStream = new FileOutputStream(logFile, false);
			byte[] myBytes = stocks.toString().getBytes();
			fileStream.write(myBytes);
			fileStream.close();
		}catch (Exception e){
			System.out.println("Stock record writing error!");
		}
	}
	public void loadStockRecord(){
		stocks.clearAllItems();
		try{
			File f = new File(thisLink);
			if(!f.exists()){
				f.createNewFile();
			}
			FileInputStream fstream = new FileInputStream(f);
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			String str = br.readLine();
			while (str != null){
				/*refactored, old controller cannot read names contain space
				 * String temp = str;
				int index = temp.indexOf(' ');
				String name = temp.substring(0, index);
				temp = temp.substring(index + 1);
				stocks.addItem(name, Integer.parseInt(temp));
				str = br.readLine();*/
				String temp = str;
				int index = temp.indexOf(' ');
				String name = temp.substring(0, index);
				temp = temp.substring(index+1);
				//check if the name contain two words
				while (temp.indexOf(' ') != -1){
					index = temp.indexOf(' ');
					name += " " + temp.substring(0, index);
					temp = temp.substring(index+1);
				}
				stocks.addItem(name, Integer.parseInt(temp));
				str = br.readLine();
			}
			in.close();
		}catch (Exception e){
			System.out.println("Stock record loading error");
			e.printStackTrace();
		}
	}
}
	